package com.chrray.position.util.http;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

public class Httputil {
    public static String httppost(String url, Map<String, String> headers, String body) {
        CloseableHttpClient client = HttpClients.createDefault();
        // 要调用的接口方法
        HttpPost post = new HttpPost(url);
        String jsonObject = "";
        HttpResponse res = null;
        try {
            StringEntity s = new StringEntity(body);
//            s.setContentType("application/json");
            post.setEntity(s);
            for (String key : headers.keySet()) {
                String value = headers.get(key);
                post.addHeader(key, value);
            }
            res = client.execute(post);
            jsonObject = EntityUtils.toString(res.getEntity(), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (IOException e) {
                System.out.println("httpclient异常");
                e.printStackTrace();
            }
        }
        return jsonObject;
    }

    public static String httpget(String url, Map<String, String> headers) {
        CloseableHttpClient client = HttpClients.createDefault();
        // 要调用的接口方法
        HttpResponse res = null;
        HttpGet get = new HttpGet(url);
        String jsonObject = "";
        try {
            if(headers!=null){
                for (String key : headers.keySet()) {
                    String value = headers.get(key);
                    get.addHeader(key, value);
                }
            }
            res = client.execute(get);
            jsonObject = EntityUtils.toString(res.getEntity(), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (IOException e) {
                System.out.println("httpclient异常");
                e.printStackTrace();
            }
        }
        return jsonObject;
    }

    //    public static String  responseToString(HttpResponse res) throws IOException {
//            return EntityUtils.toString(res.getEntity());
//    }

    public static void allowHttp(  HttpServletResponse httpServletResponse)  {
        //这里填写你允许进行跨域的主机ip
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        //允许的访问方法
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE, PATCH, TRACE, OPTIONS");
        httpServletResponse.setHeader("Access-Control-Max-Age", "3600");
        httpServletResponse.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    }


}
